package com.zenithmind.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenithmind.user.mapper.RolePermissionMapper;
import com.zenithmind.user.pojo.domain.RolePermission;
import com.zenithmind.user.service.RolePermissionService;
import com.zenithmind.common.annotation.DataCache;
import com.zenithmind.common.annotation.DataCacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    /**
     * 根据角色ID获取权限关联
     *
     * @param roleId 角色ID
     * @return 权限关联列表
     */
    @DataCache(prefix = "role:permission", key = "#roleId")
    public List<RolePermission> getByRoleId(String roleId) {
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId, roleId);
        return list(wrapper);
    }

    /**
     * 保存角色权限关系
     *
     * @param rolePermission 角色权限关系
     * @return 是否成功
     */
    @DataCacheEvict(prefix = "role:permission", key = "#rolePermission.roleId")
    public boolean saveRolePermission(RolePermission rolePermission) {
        return save(rolePermission);
    }

    /**
     * 删除角色权限关系
     *
     * @param id 关系ID
     * @return 是否成功
     */
    @DataCacheEvict(prefix = "role:permission", allEntries = true)
    public boolean removeRolePermission(String id) {
        return removeById(id);
    }

    /**
     * 根据角色ID删除所有权限关系
     *
     * @param roleId 角色ID
     * @return 是否成功
     */
    @DataCacheEvict(prefix = "role:permission", key = "#roleId")
    public boolean removeByRoleId(String roleId) {
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId, roleId);
        return remove(wrapper);
    }
}
